var sharedata = {};
var dialogBox;
var shareicon = [];
var sharetxt = [];

function list_share(ob){
  sharedata.id = $(ob).attr("data-id");
  sharedata.title = $(ob).attr("data-title");
  sharedata.text = $(ob).attr("data-text");
  sharedata.cacheimg  = 'widget://images/logo.png';
  var cacheimg = $(ob).attr("data-cacheimg");
  //sharedata.cacheimg  = $(ob).attr("data-cacheimg");
  sharedata.url  = $(ob).attr("data-url");
  if(cacheimg){
      api.imageCache({
            url: cacheimg
      }, function(ret, err) {
        if(ret.url){
         sharedata.cacheimg  = ret.url;
         doshare();
        }else{
         doshare();
        }
      });
  }else{
      doshare();
  }

}

function doshare() {
  shareicon = [];
  var  wx = api.require('wx');
  var  qq = api.require('QQPlus');
  wx.isInstalled(function(ret, err) {
     if (!ret.installed) {
       qq.installed(function(ret, err) {
          if (ret.status) {
              shareicon.push({text: 'QQ好友',icon: 'widget://image/share/qq.png'});
              shareicon.push({text: 'QQ空间',icon: 'widget://image/share/qzone.png'});
              sharetxt.push('QQ好友');
              sharetxt.push('QQ空间');
          }
          share_dialog();
       });
     }else{
       shareicon.push({text: '微信好友',icon: 'widget://image/share/wx.png'});
       shareicon.push({text: '微信朋友圈',icon: 'widget://image/share/wx-circle.png'});
       sharetxt.push('微信好友');
       sharetxt.push('微信朋友圈');
       qq.installed(function(ret, err) {
          if (ret.status) {
              shareicon.push({text: 'QQ好友',icon: 'widget://image/share/qq.png'});
              shareicon.push({text: 'QQ空间',icon: 'widget://image/share/qzone.png'});
              sharetxt.push('QQ好友');
              sharetxt.push('QQ空间');
          }
          share_dialog();
       });
     }
  });

}


function share_dialog(){

  if(shareicon.length == 0){
      var clipBoard = api.require('clipBoard');
      clipBoard.set({value: sharedata.url}, function(ret, err) {
          if (ret) {
                api.toast({msg :'分享链接已复制到粘贴版',location: 'middle'});
              } else {

             }
       });
      return;
  }

  dialogBox = api.require('dialogBox');
  dialogBox.actionMenu({
            rect:{h:150},
            texts:{cancel:'取消'},
     items: shareicon,
          styles: {
             bg: '#FFF',
             corner: 6,
             column: 4,
             itemText: {color: '#000',size: 14,marginT: 10},
             itemIcon: {size: 54},
             cancel: {bg: '#fff',color: '#333',h: 55,size: 14}
         },
      tapClose: true
  }, function(ret){

      if(ret.eventType=='cancel'){
          dialogBox.close({dialogName:'actionMenu'});
      } else if(ret.eventType=='click'){

        if(sharetxt[ret.index] == '微信好友'){
          wxshare('session');
        }
        if(sharetxt[ret.index] == '微信朋友圈'){
          wxshare('timeline');
        }
        if(sharetxt[ret.index] == 'QQ好友'){
          qqshare('QFriend');
        }
        if(sharetxt[ret.index] == 'QQ空间'){
          qqshare('QZone');
        }

      }
  });
}


function wxshare(wxscene){

var wx = api.require('wx');

console_debug(sharedata,'');

wx.isInstalled(function(ret, err) {
 if (ret.installed) {



//  cacheimg = '';

 wx.shareWebpage({
      apiKey: 'wxfbe07a05b8124c5c',
      scene: wxscene,
      title: sharedata.title,
      description: sharedata.text,
      thumb: sharedata.cacheimg,
      contentUrl: sharedata.url
 }, function(ret, err) {
    //api.hideProgress();
    if (ret.status) {
       api.toast({msg : '分享成功！',location: 'middle'});
       update_share_time();
    } else {
       alert('错误代码'+err.code);
    }

 });



 } else {
     alert('当前设备未安装微信客户端');
 }
});

}


function qqshare(ptype){

var  qq = api.require('QQPlus');
qq.installed(function(ret, err) {

 if (ret.status) {
//api.showProgress({title:"提示!",text:"分享中..."});
qq.shareNews({
 url: sharedata.url,
 title: sharedata.title,
 description: sharedata.text,
 type:ptype ,
 imgUrl: sharedata.cacheimg
}, function(ret, err) {
  //api.hideProgress();
  //api.alert({msg:JSON.stringify(ret)});
  //api.alert({msg:JSON.stringify(err)});
 if (ret.status) {

     //update_share_time();
 }
});


 } else {
     api.alert({ msg: "没有安装QQ" });
 }

});

}

function update_share_time(){
  var userinfo = get_user_info();
  if(!userinfo.Token){
     //return false;
  }
  var data1 = {"token":userinfo.Token,"data":{"id":sharedata.id,"action":'update_share_time'}};
  console_debug(data1,data1);
  api.ajax({
        url: server+'/api/ajax.php',
        method : 'post',
        returnAll : false,
        data: {body: create_post_data(data1)}
   },function(ret, err){
      console_debug(ret,err);
      if(ret.status == 1){
          api.toast({msg : ret.msg,location: 'middle'});
          //api.setPrefs({key:'userinfo',value : ret.data});
          //userinfo = ret.data;
          //api.sendEvent({name : 'userinfo_update',extra : 'update'});
      }else{

      }

   });
}
